/***************************************************************************
 创建者: 华磊
 开始时间: 2018.8.14
 copyright: (C) 华友高科
 修改说明: (每次有修改就添加一条，带有 修改人，修改时间，修改描述)
 example (1) hualei 2017.5.8 类的具体实现代码编写

 ***************************************************************************/
#include "modbusdriverconfig.h"
#include <qsettings.h>
#include <qdebug.h>

ModbusDriverConfig::ModbusDriverConfig(QString fileName)
{
    isInitialOk=false;
    if(1!=readConfigFile(fileName))
    {
        isInitialOk=false;
    }
    else
    {
        isInitialOk=true;
    }
}


int ModbusDriverConfig::readConfigFile(QString fileName)
{
    if(1!=readCommunicateParameter(fileName))
    {
        return -1;
    }

    if(1!=readRegConfigParameter(fileName))
    {
        return -2;
    }

    if(1!=readRegAddressParameter(fileName))
    {
        return -3;
    }

    if(1!=readVendorId(fileName))
    {
        return -4;
    }


    qDebug() << "ModbusDriverConfig::readConfigFile sucess";
    return 1;

}


int ModbusDriverConfig::readCommunicateParameter(QString fileName)
{
    QVariant strConfigValue;
    QString firstTimeFlagStr = fileName;
    QString strConfigGroup = "";
    QString strConfigKey = "";

    QSettings settings(firstTimeFlagStr,QSettings::IniFormat);
    settings.setIniCodec("UTF-8");
    strConfigGroup = "CommunicateInfo";
    settings.beginGroup(strConfigGroup);

    //modbusType
    strConfigKey = "modbusType";
    if(false==settings.contains(strConfigKey))
    {
        return -1;
    }
    strConfigValue = settings.value(strConfigKey);
    comParameter.modbusType = (E_MODBUS_TYPE)strConfigValue.toInt();

    //ip
    strConfigKey = "ip";
    if(false==settings.contains(strConfigKey))
    {
        return -2;
    }
    strConfigValue = settings.value(strConfigKey);
    comParameter.ip = strConfigValue.toString();

    //port
    strConfigKey = "port";
    if(false==settings.contains(strConfigKey))
    {
        return -3;
    }
    strConfigValue = settings.value(strConfigKey);
    comParameter.port = strConfigValue.toInt();


    //serialDeviceName
    strConfigKey = "serialDeviceName";
    if(false==settings.contains(strConfigKey))
    {
        return -4;
    }
    strConfigValue = settings.value(strConfigKey);
    comParameter.serialDeviceName = strConfigValue.toString();

    //parity
    strConfigKey = "parity";
    if(false==settings.contains(strConfigKey))
    {
        return -5;
    }
    strConfigValue = settings.value(strConfigKey);
    comParameter.parity = strConfigValue.toString();

    //baud
    strConfigKey = "baud";
    if(false==settings.contains(strConfigKey))
    {
        return -6;
    }
    strConfigValue = settings.value(strConfigKey);
    comParameter.baud = strConfigValue.toInt();

    //data_bit
    strConfigKey = "data_bit";
    if(false==settings.contains(strConfigKey))
    {
        return -7;
    }
    strConfigValue = settings.value(strConfigKey);
    comParameter.data_bit = strConfigValue.toInt();

    //stop_bit
    strConfigKey = "stop_bit";
    if(false==settings.contains(strConfigKey))
    {
        return -8;
    }
    strConfigValue = settings.value(strConfigKey);
    comParameter.stop_bit = strConfigValue.toInt();

    settings.endGroup();

    qDebug() << "ModbusDriverConfig::readCommunicateParameter sucess";
    return 1;
}


int ModbusDriverConfig::readRegConfigParameter(QString fileName)
{
    QVariant strConfigValue;
    QString firstTimeFlagStr = fileName;
    QString strConfigGroup = "";
    QString strConfigKey = "";

    QSettings settings(firstTimeFlagStr,QSettings::IniFormat);
    settings.setIniCodec("UTF-8");
    strConfigGroup = "RegConfig";
    settings.beginGroup(strConfigGroup);

    //ratedCurrency
    strConfigKey = "ratedCurrency";
    if(false==settings.contains(strConfigKey))
    {
        return -1;
    }
    strConfigValue = settings.value(strConfigKey);
    regConfig.ratedCurrency = strConfigValue.toInt();

    //maxCurrency
    strConfigKey = "maxCurrency";
    if(false==settings.contains(strConfigKey))
    {
        return -2;
    }
    strConfigValue = settings.value(strConfigKey);
    regConfig.maxCurrency = strConfigValue.toInt();

    //watchDogTimeOut
    strConfigKey = "watchDogTimeOut";
    if(false==settings.contains(strConfigKey))
    {
        return -3;
    }
    strConfigValue = settings.value(strConfigKey);
    regConfig.watchDogTimeOut = strConfigValue.toInt();

    //driverModel
    strConfigKey = "driverModel";
    if(false==settings.contains(strConfigKey))
    {
        return -4;
    }
    strConfigValue = settings.value(strConfigKey);
    regConfig.driverModel = strConfigValue.toInt();


    settings.endGroup();

    qDebug() << "ModbusDriverConfig::readRegConfigParameter sucess";
    return 1;
}

int ModbusDriverConfig::readVendorId(QString fileName)
{
    QVariant strConfigValue;
    QString firstTimeFlagStr = fileName;
    QString strConfigGroup = "";
    QString strConfigKey = "";

    QSettings settings(firstTimeFlagStr,QSettings::IniFormat);
    settings.setIniCodec("UTF-8");
    strConfigGroup = "GeneralInfo";
    settings.beginGroup(strConfigGroup);

    //vendorId
    strConfigKey = "vendorId";
    if(false==settings.contains(strConfigKey))
    {
        return -1;
    }
    strConfigValue = settings.value(strConfigKey);
    vendorId= (E_VENDOR_ID)strConfigValue.toInt();


    settings.endGroup();

    qDebug() << "ModbusDriverConfig::readVendorId sucess";
    return 1;
}

int ModbusDriverConfig::readRegAddressParameter(QString fileName)
{
    QVariant strConfigValue;
    QString firstTimeFlagStr = fileName;
    QString strConfigGroup = "";
    QString strConfigKey = "";

    QSettings settings(firstTimeFlagStr,QSettings::IniFormat);
    settings.setIniCodec("UTF-8");
    strConfigGroup = "RegAddressInfo";
    settings.beginGroup(strConfigGroup);

    //进制转化
//#include <stdio.h>
//void main()
//{
//    char* p = "0x1a";
//    int nValude = 0;
//    sscanf(p, "%x", &nValude);
//    printf("%d\r\n", nValude);
//}

    //pwmControlAdd
    strConfigKey = "pwmControlAdd";
    if(false==settings.contains(strConfigKey))
    {
        return -1;
    }
    strConfigValue = settings.value(strConfigKey);
    regAdd.pwmControlAdd = strConfigValue.toInt();

    //speedControlAdd
    strConfigKey = "speedControlAdd";
    if(false==settings.contains(strConfigKey))
    {
        return -2;
    }
    strConfigValue = settings.value(strConfigKey);
    regAdd.speedControlAdd = strConfigValue.toInt();

    //positionControlSpeedAdd
    strConfigKey = "positionControlSpeedAdd";
    if(false==settings.contains(strConfigKey))
    {
        return -3;
    }
    strConfigValue = settings.value(strConfigKey);
    regAdd.positionControlSpeedAdd = strConfigValue.toInt();

    //positionControlPositionHighAdd
    strConfigKey = "positionControlPositionHighAdd";
    if(false==settings.contains(strConfigKey))
    {
        return -4;
    }
    strConfigValue = settings.value(strConfigKey);
    regAdd.positionControlPositionHighAdd = strConfigValue.toInt();

    //positionControlPositionLowAdd
    strConfigKey = "positionControlPositionLowAdd";
    if(false==settings.contains(strConfigKey))
    {
        return -5;
    }
    strConfigValue = settings.value(strConfigKey);
    regAdd.positionControlPositionLowAdd = strConfigValue.toInt();

    //ratedCurrencyAdd
    strConfigKey = "ratedCurrencyAdd";
    if(false==settings.contains(strConfigKey))
    {
        return -6;
    }
    strConfigValue = settings.value(strConfigKey);
    regAdd.ratedCurrencyAdd = strConfigValue.toInt();

    //maxCurrencyAdd
    strConfigKey = "maxCurrencyAdd";
    if(false==settings.contains(strConfigKey))
    {
        return -7;
    }
    strConfigValue = settings.value(strConfigKey);
    regAdd.maxCurrencyAdd = strConfigValue.toInt();

    //watchDogTimeOutAdd
    strConfigKey = "watchDogTimeOutAdd";
    if(false==settings.contains(strConfigKey))
    {
        return -8;
    }
    strConfigValue = settings.value(strConfigKey);
    regAdd.watchDogTimeOutAdd = strConfigValue.toInt();

    //driverModelAdd
    strConfigKey = "driverModelAdd";
    if(false==settings.contains(strConfigKey))
    {
        return -9;
    }
    strConfigValue = settings.value(strConfigKey);
    regAdd.driverModelAdd = strConfigValue.toInt();

    //motorCurrentSpeedFeedbackAdd
    strConfigKey = "motorCurrentSpeedFeedbackAdd";
    if(false==settings.contains(strConfigKey))
    {
        return -10;
    }
    strConfigValue = settings.value(strConfigKey);
    regAdd.motorCurrentSpeedFeedbackAdd = strConfigValue.toInt();

    //motorCurrentCurrencyFeedbackAdd
    strConfigKey = "motorCurrentCurrencyFeedbackAdd";
    if(false==settings.contains(strConfigKey))
    {
        return -11;
    }
    strConfigValue = settings.value(strConfigKey);
    regAdd.motorCurrentCurrencyFeedbackAdd = strConfigValue.toInt();

    //motorCurrentPositionFeedbackHighAdd
    strConfigKey = "motorCurrentPositionFeedbackHighAdd";
    if(false==settings.contains(strConfigKey))
    {
        return -12;
    }
    strConfigValue = settings.value(strConfigKey);
    regAdd.motorCurrentPositionFeedbackHighAdd = strConfigValue.toInt();

    //motorCurrentPositionFeedbackLowAdd
    strConfigKey = "motorCurrentPositionFeedbackLowAdd";
    if(false==settings.contains(strConfigKey))
    {
        return -13;
    }
    strConfigValue = settings.value(strConfigKey);
    regAdd.motorCurrentPositionFeedbackLowAdd = strConfigValue.toInt();

    //errorCodeAdd
    strConfigKey = "errorCodeAdd";
    if(false==settings.contains(strConfigKey))
    {
        return -14;
    }
    strConfigValue = settings.value(strConfigKey);
    regAdd.errorCodeAdd = strConfigValue.toInt();

    //deviceModelTypeAdd
    strConfigKey = "deviceModelTypeAdd";
    if(false==settings.contains(strConfigKey))
    {
        return -15;
    }
    strConfigValue = settings.value(strConfigKey);
    regAdd.deviceModelTypeAdd = strConfigValue.toInt();


    settings.endGroup();

    qDebug() << "ModbusDriverConfig::readRegAddressParameter sucess";
    return 1;

}

bool ModbusDriverConfig::isInitialOkStatus()
{
    return isInitialOk;
}

int ModbusDriverConfig::getParameter(CommunicateParameter& comParameterReturn, RegConfig& regConfigReturn,
                 RegAddressInfo& regAddReturn, E_VENDOR_ID &vendorIdReturn)
{
    if(true==isInitialOk)
    {
        comParameterReturn=comParameter;
        regConfigReturn=regConfig;
        regAddReturn=regAdd;
        vendorIdReturn=vendorId;
        return 1;
    }
    else
    {
        return -1;
    }
}

int ModbusDriverConfig::getCommunicateParameter(CommunicateParameter& comParameterReturn)
{
    if(true==isInitialOk)
    {
        comParameterReturn=comParameter;
        return 1;
    }
    else
    {
        return -1;
    }

}

int ModbusDriverConfig::getRegConfigParameter(RegConfig& regConfigReturn)
{
    if(true==isInitialOk)
    {
        regConfigReturn=regConfig;
        return 1;
    }
    else
    {
        return -1;
    }
}

int ModbusDriverConfig::getRegAddressParameter(RegAddressInfo& regAddReturn)
{
    if(true==isInitialOk)
    {
        regAddReturn=regAdd;
        return 1;
    }
    else
    {
        return -1;
    }
}

int ModbusDriverConfig::getVendorId(int& vendorIdReturn)
{
    if(true==isInitialOk)
    {
        vendorIdReturn=vendorId;
        return 1;
    }
    else
    {
        return -1;
    }
}



